package com.charlee.controller;

import com.charlee.entity.User;
import com.charlee.service.UserService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by charlee
 * on 17-6-10
 * version v1.0.0
 */
@Controller
@RequestMapping("/user")
@Scope("prototype")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("/hello2")
    public String hello2(Model model) {
        model.addAttribute("msg", "hello world!");
        return "hello";
    }

    @GetMapping("/hello1")
    public ModelAndView hello1(ModelAndView mv) {
        mv.addObject("msg", "hello world!");
        mv.setViewName("hello");
        return mv;
    }

    @PostMapping("/getUser")
    @ResponseBody
    public User getUser(String username, String password) {
        return userService.findUser(username, password);
    }

    @GetMapping("/getUsers")
    @ResponseBody
    public List<User> users() {
        return userService.findAllUser();
    }
}